package it.marcelate.rcp.jdtutil.ui.actions;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.core.SourceType;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
 * 
 * @author demarcom
 *
 * @see http://www.jdg2e.com/ch27.jdt/doc/index.html (The Java Developers Guide to Eclipse # ch. 27)
 * @see http://www.jdg2e.com/jdg2e_CD_for_eclipse321.zip (download examples)
 *
 *
 */


public class TraceAction implements IWorkbenchWindowActionDelegate {

  private Object selected;
  
  @Override
  public void dispose() {

  }

  @Override
  public void init(IWorkbenchWindow window) {

  }

  @Override
  public void run(IAction action) {
    try {
      System.out.println("======> Tracing types");
      if (selected != null) {
        
        if (selected instanceof ICompilationUnit) {
          
          ICompilationUnit cu = (ICompilationUnit)selected;
          
          IType[] types = cu.getAllTypes();
          for (int it = 0; it < types.length; it++) {
            IType type = types[it];
            System.out.println("Type class = " + type.getClass() + " name = " + type.getFullyQualifiedName());
            
            if (type instanceof SourceType) {
              SourceType st = (SourceType)type;
              
              System.out.println("======> Tracing fields");
              IField[] fields = st.getFields();
              for (int iff = 0; iff < fields.length; iff++) {
                IField field = fields[iff];
                System.out.println("Field name " + field.getElementName());
              }
              
              System.out.println("======> Tracing methods");
              IMethod[] methods = st.getMethods();
              for (int im = 0; im < methods.length; im++) {
                IMethod method = methods[im];
                System.out.println("Method name " + method.getElementName());
              }
              
            }
          }
          
        }
        
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public void selectionChanged(IAction action, ISelection selection) {
    try {
      System.out.println("======> selectionChanged");
      if (selection == null) {
        System.out.println("<null selection>");
      } else {
        System.out.println("Selection is of class " + selection.getClass());
        if (selection instanceof IStructuredSelection) {
          IStructuredSelection sel = (IStructuredSelection)selection;
          if (sel.isEmpty()) {
            System.out.println("<empty selection>");
          } else {
            selected = sel.getFirstElement();
            System.out.println("First element is of class " + selected.getClass());
          }
        } else if (selection instanceof ITextSelection) {
          ITextSelection sel = (ITextSelection)selection;
          System.out.println("Text selected is " + sel.getText());
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    
      
  }

}
